<?php

namespace app\adminmini\service;


use app\adminmini\model\Course;
use think\facade\Db;

class CourseService extends BaseService
{
    /**
     * 构造函数
     * @author 牧羊人
     * @since 2020/11/15
     * MemberLevelService constructor.
     */
    public function __construct()
    {
        $this->model = new Course();
    }
public function subscribe(){
    $data = request()->param();
    //subscribe
    Db::name('customer_course')->where(['id' => $data['id']])->update(['subscribe'=>$data['subscribe']]);
    return message('操作成功', true);
}
    public function edit()
    {
        $data = request()->param();
        $data['store_id'] = $this->userId();
        $label = $data['label_id'];
        // 插入用户角色关系数据
        $roleIds = isset($label) ? $label : [];
        unset($data['label_id']);

        $package_id = $data['package_id'];
        // 插入用户角色关系数据
        $package_id = isset($package_id) ? $package_id : [];
        unset($data['package_id']);


        // 文章图集
        $imgStr = [];
        $imgsList = isset($data['image_url']) ? $data['image_url'] : [];
        if ($imgsList) {
            foreach ($imgsList as $val) {
                if (strpos($val, "temp")) {
                    //新上传图片
                    $imgStr[] = save_image($val, 'article');
                } else {
                    //过滤已上传图片
                    $imgStr[] = str_replace(IMG_URL, "", $val);
                }
            }
            $data['image_url'] = serialize($imgStr);
        }


        $result = $this->model->edit($data, $error);
        Db::name('customer_course_label')->where('course_id', $result)->delete();
        $this->insertlabel($result, $roleIds);
        Db::name('customer_course_package_id')->where('course_id', $result)->delete();
        $this->insertpackage($result, $package_id);
        cache('course'.$result,false);
        return message('操作成功', true);
    }

    /**
     * @param $result
     * @param $roleIds
     * @return void
     */
    public function insertlabel($result, $roleIds)
    {
        if (!empty($roleIds)) {
            $list = [];
            foreach ($roleIds as $val) {
                $data = [
                    'course_id' => $result,
                    'lable_id' => $val,
                ];
                $list[] = $data;
            }
            Db::name('customer_course_label')->insertAll($list);
        }
    }

    /**
     * @param $result
     * @param $roleIds
     * @return void
     */
    public function insertpackage($result, $roleIds)
    {


        if (!empty($roleIds)) {
            $list = [];
            foreach ($roleIds as $val) {
                $data = [
                    'course_id' => $result,
                    'package_id' => $val,
                ];
                $list[] = $data;
            }
            Db::name('customer_course_package_id')->insertAll($list);
        }
    }


    /**
     * @param $userId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getlabel($userId)
    {
        $roleList = Db::name('customer_course_label')->alias("ur")
            ->field('r.*')
            ->join('mini_lable r', 'ur.lable_id=r.id')
            ->distinct(true)
            ->where('ur.course_id', '=', $userId)
            ->select()->toArray();
        return $roleList;
    }

    /**
     * @param $userId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getpackage($userId)
    {
        $roleList = Db::name('customer_course_package_id')->alias("ur")
            ->field('r.*')
            ->join('customer_package r', 'ur.package_id=r.id')
            ->distinct(true)
            ->where('ur.course_id', '=', $userId)
            ->select()->toArray();
        return $roleList;
    }

    /**
     * @return array
     */
    public function getList()
    {
        $param = request()->param();

        // 查询条件
        $map = [];


        // 名称
        $name = isset($param['name']) ? trim($param['name']) : '';
        if ($name) {
            $map[] = ['name', 'like', "%{$name}%"];
        }
        $map[] = ['mark', '=', 1];
        $map[] = ['store_id', '=', $this->userId()];
        $list = Db::name('customer_course')
            ->where($map)->page(PAGE, PERPAGE)
            ->select();

        foreach ($list as $i => $v) {
            $label = $this->getlabel($v['id']);
            if (count($label) > 0) {
                $v['label_id'] = $label;
            } else {
                $v['label_id'] = [];
            }

            $package_id = $this->getpackage($v['id']);

            $name = array();
            if (count($package_id) > 0) {
                $v['package_id'] = $package_id;
                foreach ($package_id as $k => $value) {
                    $name[] = $value['name'];
                }
                $v['package_name'] = $this->arr_to_str($name);
            } else {
                $v['package_id'] = [];
            }
            if (isset($v['image_url']) && $v['image_url'] != "") {

                if ($v['image_url'] != "") {
                    $imgsList = unserialize($v['image_url']);
                    foreach ($imgsList as &$val) {
                        $val = get_image_url($val);
                    }
                    $v['image_url'] = $imgsList;
                } else {
                    $v['image_url'] = '';
                }

            } else {
                $v['image_url'] = '';
            }
            $v['door_name'] = '';
            if ($v['door_id'] > 0) {


                $door = Db::name('customer_door_list')->where('id', $v['door_id'])->find();
            if(! isset($door['name']))   {

                var_dump($v['door_id']);exit();
            }
                $v['door_name'] = $door['name'];

            }
            $list[$i] = $v;
        }
        $count = Db::name('customer_course')
            ->where($map)->page(PAGE, PERPAGE)
            ->count();

        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $list,
            "count" => $count,
        );
        return $message;
    }

    public
    function arr_to_str($arr)
    {
        $t = '';
        foreach ($arr as $v) {
            $temp[] = $v;
        }
        foreach ($temp as $v) {
            $t .= $v . ',';
        }
        $t = substr($t, 0, -1); // 利用字符串截取函数消除最后一个逗号
        return $t;
    }
}